package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JInternalFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Delete extends JInternalFrame {
	Connection con;
	DbTest dt;
	private JTextField serial;
	private JTextField code;
	private JTextField ea;
	private JButton button;
	private JButton button_1;
	private JLabel status;
	private JLabel lblNewLabel;
	private JLabel lb1;
	private JLabel lblCode;
	private JLabel lblEa;
	/**
	 * Launch the application.
	 */

	/**
	 * Create the frame.
	 */
	public Delete(Connection c,DbTest dt) {
		super("Delete",true,true,true,false);
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getSerial());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getButton());
		getContentPane().add(getButton_1());
		getContentPane().add(getStatus());
	
		getContentPane().add(getLb1());
		getContentPane().add(getLblCode());
		getContentPane().add(getLblEa());
		con=c;
		this.dt=dt;
	}

	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setColumns(10);
			serial.setBounds(143, 33, 116, 33);
		}
		return serial;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setColumns(10);
			code.setBounds(143, 76, 116, 33);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(143, 119, 116, 33);
		}
		return ea;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("삭제");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					try{
						int s=Integer.parseInt(serial.getText());
						String sql="delete from ptbl where serial =?";
						PreparedStatement pstmt=con.prepareStatement(sql);
						pstmt.setInt(1, s);
//						ResultSet rs=pstmt.executeQuery();
						
						pstmt.executeUpdate();
						pstmt.close();
						serial.setText("");
						code.setText("");
						ea.setText("");
					}catch(Exception ex){
						
					}
				
				}
			});
			button.setBounds(48, 176, 97, 23);
		}
		return button;
	}
	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("검색");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int s=Integer.parseInt(serial.getText());
					String sql="select * from ptbl where serial =?";
					try{
						PreparedStatement pstmt=con.prepareStatement(sql);
						pstmt.setInt(1, s);
						ResultSet rs=pstmt.executeQuery();
						if(rs.next()){
							code.setText(rs.getString("code"));
							ea.setText(""+rs.getInt("ea"));
						}else{
							status.setText("데이터가 없습니다.");
						}
						
						rs.close();
						pstmt.close();
					}catch(Exception ex){
						
					}
				}
			});
			button_1.setBounds(178, 176, 97, 23);
		}
		return button_1;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("New label");
			status.setBounds(12, 246, 399, 15);
		}
		return status;
	}
	private JLabel getLb1() {
		if (lb1 == null) {
			lb1 = new JLabel("SetialNumber");
			lb1.setBounds(32, 42, 99, 15);
		}
		return lb1;
	}
	private JLabel getLblCode() {
		if (lblCode == null) {
			lblCode = new JLabel("Code");
			lblCode.setBounds(32, 85, 99, 15);
		}
		return lblCode;
	}
	private JLabel getLblEa() {
		if (lblEa == null) {
			lblEa = new JLabel("EA");
			lblEa.setBounds(32, 128, 99, 15);
		}
		return lblEa;
	}
}
